<template>
	<view :class="['page',{'iphoneX':isIphoneX}]">
		<template v-if="isLogin">
			<view class="map">
				<map style="width: 100%; height: 400rpx;" :polyline="polylines" scale="16" :latitude="params.latitude"
					:longitude="params.longitude" :markers="markers" :circles="circles">
				</map>
				<cover-view class="get-local-btn" @click="getLocal">
					<cover-image class="local-img" src="@/static/images/get_local_icon.png"></cover-image>
				</cover-view>
			</view>

			<!-- 作业中 -->
			<template v-if="work_type=='work'">
				<view class="body">
					<view class="form-wrap m-b2">
						<view class="form-item">
							<view class="label">地 址：</view>
							<view class="form-input">
								<view class="content">
									<template v-if="params.address">
										<textarea class="inp" auto-height disabled v-model="params.address"
											placeholder="输入详细地址" maxlength="-1"></textarea>
									</template>
									<template v-else>
										<view class="inp f-grey">获取位置中...</view>
									</template>

								</view>
							</view>
						</view>

						<!-- <view class="form-item" v-if="params.address">
							<view class="label">定位精度：</view>
							<view class="form-input">
								<view class="content">
									<view class="inp">{{test}}</view>
								</view>
							</view>
						</view> -->

						<view class="form-item">
							<view class="label">选择生境类型：</view>
							<template v-if="params.envtype">
								<picker class="form-picker" mode="selector" :range="envtypes" @change="changeEnvtypes">
									<view class="picker">
										<input type="text" class="select-data t-l" v-model="params.envtype"
											placeholder="选择生境类型" disabled>
										<view class="icon">
											<view class="iconfont f-s68">&#xeb8a;</view>
										</view>
									</view>
								</picker>
							</template>
							<template v-else>
								<view class="form-input">
									<view class="content">
										<input type="text" class="inp" v-model="params.envtype" placeholder="输入生境类型" />
									</view>
								</view>
							</template>
						</view>

						<view class="form-item">
							<view class="label">活蚁巢数量：</view>
							<view class="form-input">
								<view class="content">
									<input type="digit" class="inp" v-model="params.nestNum" placeholder="输入活蚁巢数量"
										disabled />
									<view class="link">个</view>
									<!-- <navigator open-type="navigate" hover-class="none"
										:url="'/package_project/jc_pro/setlocal/setlocal?&lat='+params.latitude+'&lng='+params.longitude+'&level='+params.leavel+'&leavel_num='+params.leavel_num+'&fid='+params.fid+'&pid='+params.pid+'&nestNum='+params.nestNum+'&accuracy='+params.accuracy+'&type='+params.type"
										class="local-btn">去定位</navigator> -->
									<view class="local-btn" @click="goSetLocal">去定位</view>
								</view>
							</view>
						</view>

						<view class="form-item">
							<view class="label">诱集器数量：</view>
							<view class="form-input">
								<view class="content">
									<input type="digit" class="inp" v-model="params.trappers_count"
										placeholder="输入诱集器数量(选填)" @input="setTrappersCount" />
									<view class="link">个</view>
								</view>
							</view>
						</view>
					</view>

					<template v-if="params.trappers_count>0">
						<view class="hd-title">诱集器工蚁数</view>
						<view class="form-wrap m-b2">
							<view class="form-item" v-for="(item,index) in params.trappers_count" :key="index">
								<view class="label">诱集器-{{index+1}}：</view>
								<view class="form-input">
									<view class="content">
										<input type="digit" class="inp" v-model="params.ant_counts[index+1]"
											:placeholder="'输入诱集器-'+(index+1)+'工蚁数量'" />
										<view class="link">个</view>
									</view>
								</view>
							</view>
						</view>
					</template>

					<!-- 	<view v-if="params.trappers_count>0" class="form-wrap m-b2">
						<view class="hd-title">诱集器工蚁数</view>
						<view class="form-item" v-for="(item,index) in params.trappers_count">
							<view class="label">诱集器-{{index+1}}：</view>
							<view class="form-input">
								<view class="content">
									<input type="digit" class="inp" v-model="params.trappers_count" placeholder="输入诱集工蚁数量" />
									<view class="link">个</view>
								</view>
							</view>
						</view> 
					</view> -->

					<!-- 图片上传 -->
					<view class="form-wrap m-b2">
						<view class="upload-pic-item">
							<view class="label">现场拍照</view>
							<view class="form-upload-pic">
								<template>
									<view class="img-item" v-for="(item,index) in params.files" :key="index">
										<image src="@/static/images/filter_bg.png" mode="widthFix">
											<!-- 占位 -->
										</image>
										<view class="img-wrap" @click="previewPic(index)">
											<image class="img" :src="item.status==1?$imgPath(item.file_path):item.url"
												mode="widthFix"></image>
										</view>
										<view class="del-icon" @click="delPic(index)">
											<view class="iconfont">&#xe74b;</view>
										</view>
										<view v-if="item.status==0" class="loading-img">
											<div class="loading-icon loader-18"></div>
										</view>
									</view>
								</template>
								<view class="img-item add-btn" @click="uploadImage">
									<image src="@/static/images/upload_pic.png" mode="widthFix"></image>
								</view>
							</view>
						</view>
					</view>

					<view class="form-wrap m-b2">
						<view class="form-textarea-item">
							<view class="label a-t">备注信息</view>
							<view class="form-textarea">
								<textarea class="inp-textarea" v-model="params.remark" placeholder="备注信息(选填),如地块名称等"
									maxlength="-1"></textarea>
							</view>
						</view>
					</view>

				</view>

				<!-- 底部 -->
				<view class="footer">
					<view class="fix-wrap bottom">
						<view class="btn" @click="submit">提交</view>
					</view>
				</view>
			</template>

			<!-- 蚁巢定位 -->
			<template v-if="work_type=='local'">
				<view class="body">
					<view class="form-wrap m-b2">

						<view class="form-item">
							<view class="label">经纬度：</view>
							<view class="form-input">
								<view class="content">
									<input type="text" class="inp" :value="params.localtion" placeholder="" disabled />

								</view>
							</view>
						</view>

						<view class="form-item">
							<view class="label">活蚁巢数量：</view>
							<view class="form-input">
								<view class="content">
									<input type="text" class="inp" v-model="params.nestNum" placeholder="活蚁巢数量"
										disabled />
									<view class="link">个</view>
								</view>
							</view>
						</view>

					</view>

					<view class="set-local-btn" @click="setYcLocal">定位</view>

				</view>

				<!-- 底部 -->
				<view class="footer">
					<view class="fix-wrap bottom">
						<view class="form-lay-btn" @click="backWork">定位完成，返回继续作业</view>
					</view>
				</view>
			</template>

		</template>
	</view>
</template>

<script>
	import {
		dateFormat,
		uploadImage,
		previewImage,
		uploadToServe,
		d2Dms
	} from '@/common/util.js'
	import {
		gd_map_Key,
		gd_map_Key_web
	} from '@/config.js'
	export default {
		name: 'name',
		data() {
			return {
				test: '',
				isLogin: true,
				isIphoneX: getApp().globalData.isIphoneX,
				userInfo: uni.getStorageSync('userInfo'),
				isStop: true,
				envtypes: [],
				isSaveData: false, // 是否缓存数据	
				prev_data: null,
				work_type: 'work',
				params: {
					type: 'jc',
					pid: 0, // 项目作业时为 项目id    群作业时  群id    pid为0  或者  群作业为公共作业
					pro_name: '',
					work_type: '',
					fid: '0',
					accuracy: '',
					envtype: '' || uni.getStorageSync('pro_jc_data').env_type,
					nestNum: 0,
					trappers_count: '',
					ant_counts: {},
					speed: '',
					address: '',
					files: [],
					remark: '' || uni.getStorageSync('pro_jc_data').remark,
					leavel: '',
					leavel_num: 0,
					gcj02_lat: '',
					gcj02_lng: '',
					latitude: '',
					longitude: '',
					altitude: '',
					prevlatitude: '',
					prevlongitude: '',
					yc_prevlatitude: '',
					yc_prevlongitude: '',
					localtion: '',
					times1: 1
				},
				markers: [],
				circles: [{
					latitude: '',
					longitude: '',
					color: "#ff00000A",
					fillColor: "#ff00003A",
					radius: "50",
				}],
				polylines: [{
					points: [],
					color: "#fcd535",
					width: 10
				}],
				timer: null,
				local_timer: null,
				marker_id: 1,
				loading: false
			}


		},
		async onLoad(params) {

			if (params.type) {
				this.params.type = params.type;
			}
			if (params.pid) {
				this.params.pid = params.pid;
			}
			if (params.pro_name) {
				this.params.pro_name = params.pro_name;
			}
			if (params.work_type) {
				this.params.work_type = params.work_type;
			}

			this.isLogin = await this.$loginState('check');
			this.prev_data = uni.getStorageSync('prev_jc_' + this.params.pid); // 上次未提交数据

			uni.setNavigationBarTitle({
				title: params.pro_name
			})


			if (this.isLogin) {
				let _this = this;
				this.getLocal();

				this.getEnvtype();

				if (this.prev_data) {
					uni.showModal({
						content: '有未提交的作业，是否继续上一次作业',
						success(res) {
							if (res.confirm) {


								if (uni.getStorageSync('markers_' + params.pid).length > 0) {
									_this.markers = uni.getStorageSync('markers_' + params.pid);
								}

								_this.params.nestNum = _this.prev_data.nestNum;
								_this.params.trappers_count = _this.prev_data.trappers_count;
								_this.params.files = _this.prev_data.files;
								_this.params.ant_counts = _this.prev_data.ant_counts;
								_this.params.fid = _this.prev_data.fid;

							} else {
								uni.removeStorageSync('prev_jc_' + _this.params.pid);
								uni.removeStorageSync('markers_' + _this.params.pid); // 清除蚁巢地图marker点
								_this.params.nestNum = 0;
							}
						}
					})
				}
			}
		},
		async onShow() {

			let _this = this;
			// 10s获取1次位置
			this.timer = setInterval(() => {
				_this.updateLocus();
			}, 10000)






		},
		onHide() {
			if (!this.isSaveData && (this.params.nestNum > 0 || this.params.trappers_count || this.params.files.length > 0)) { 
				uni.setStorage({
					key: 'prev_jc_' + this.params.pid,
					data: {
						nestNum: this.params.nestNum,
						trappers_count: this.params.trappers_count,
						files: this.params.files,
						ant_counts: this.params.ant_counts,
						fid: this.params.fid
					}
				})
			}
			clearInterval(this.timer); // 关闭轮询位置定时器 
		},
		onUnload() {
			if (!this.isSaveData && (this.params.nestNum > 0 || this.params.trappers_count || this.params.files.length > 0)) {
				uni.setStorage({
					key: 'prev_jc_' + this.params.pid,
					data: {
						nestNum: this.params.nestNum,
						trappers_count: this.params.trappers_count,
						files: this.params.files,
						ant_counts: this.params.ant_counts,
						fid: this.params.fid
					}
				})
			}
			clearInterval(this.timer); // 关闭轮询位置定时器

		},
		methods: {

			// 返回作业界面
			backWork: function() {
				this.work_type = "work";
			},

			// 切换蚁巢定位界面
			goSetLocal: function() {
				this.work_type = "local";
				// 获取当前位置
				this.params.localtion = d2Dms(parseFloat(this.params.longitude)) + '，' + d2Dms(parseFloat(this.params
					.latitude));
			},

			//蚁巢定位上传
			setYcLocal: async function() {
				let _this = this;

				if (this.loading) {
					uni.showToast({
						icon: "none",
						title: '两次定位间隔时间太短',
						mask: true,
						duration: 1000,
					})
					return
				}

				uni.showLoading({
					title: '定位中...',
					mask: true
				})

				if (this.isStop) {
					this.isStop = false;
					this.updateGPS();
				}


			},

			updateGPS: function() {
				let _this = this;
				uni.getLocation({
					type: 'gcj02',
					isHighAccuracy: true,
					altitude: true,
					highAccuracyExpireTime: 5000,
					accuracy: 50,
					async success(res) {
						_this.circles[0].latitude = _this.params.latitude;
						_this.circles[0].longitude = _this.params.longitude;
						console.log('res.accuracy', res.accuracy)
						//  判断是否相同地址范围 设置位置偏差
						let difx = Math.abs(_this.params.yc_prevlongitude - res.longitude);
						let dify = Math.abs(_this.params.yc_prevlatitude - res.latitude);

						if (res.accuracy < 30) {


							if ((_this.params.yc_prevlongitude == '' || _this.params.yc_prevlatitude ==
									'') || (difx > 0.00005 && difx < 0.0035) || (dify > 0.00005 && dify <
									0.0035)) {
								console.log('位置更新')


								_this.params.speed = res.speed;
								_this.params.accuracy = res.accuracy;
								_this.params.latitude = res.latitude;
								_this.params.longitude = res.longitude;

								let local_res = await _this.$api.pro_api.pcnestUpdateGps({
									'type': 1,
									'fid': _this.params.fid,
									'p_id': _this.params.pid,
									'speed': _this.params.speed,
									'accuracy': _this.params.accuracy,
									'gcj02_lat': res.latitude,
									'gcj02_lng': res.longitude,
									'create_uid': _this.userInfo.id,
									'timesid': 0
								});
								if (local_res.data.ret == 0) {
									uni.hideLoading();
									_this.params.nestNum += 1;
									_this.params.accuracy = res.accuracy;
									_this.params.latitude = res.latitude;
									_this.params.longitude = res.longitude;
									_this.params.localtion = d2Dms(parseFloat(res.longitude)) + '，' +
										d2Dms(parseFloat(res.latitude));


									_this.circles[0].latitude = res.latitude;
									_this.circles[0].longitude = res.longitude;

									_this.markers[0] = {
										id: 0,
										latitude: parseFloat(res.latitude),
										longitude: parseFloat(res.longitude),
										iconPath: '/static/images/my_local.png',
									}


									_this.marker_id += 1;

									let marker = {
										id: _this.marker_id,
										latitude: parseFloat(res.latitude),
										longitude: parseFloat(res.longitude),
										iconPath: '/static/images/mark9.png'
									}

									_this.markers.push(marker);

									uni.setStorage({
										key: 'markers_' + _this.params.pid,
										data: _this.markers
									})

									uni.showToast({
										icon: "success",
										title: "定位成功",
										mask: true,
										duration: 1000,
										success() {
											_this.params.yc_prevlongitude = res.longitude;
											_this.params.yc_prevlatitude = res.latitude;
											_this.loading = true;
											setTimeout(() => {
												_this.loading = false;
												_this.isStop = true;
											}, 2000)
										}
									})

								} else {
									uni.hideLoading();

									uni.showToast({
										icon: "success",
										title: res.data.message,
										mask: true,
										duration: 1000,
										success() {
											setTimeout(() => {
												_this.isStop = true;
											}, 10000)
										}
									})
								}

							} else {
								console.log('位置无更新')
								uni.showToast({
									icon: "none",
									title: '请勿在同个位置重复提交',
									mask: true,
									duration: 1000,
								})

								uni.hideLoading();
								_this.local_timer = setTimeout(() => {
									_this.isStop = true;
								}, 1000)
							}
						} else {
							_this.local_timer = setTimeout(() => {
								_this.isStop = true;
								_this.updateGPS();
							}, 1000)
						}

					},
					fail(err) {
						console.log(err)
						uni.hideLoading();
						uni.showToast({
							image: '../../static/images/error.png',
							title: "定位失败",
							mask: true,
							duration: 1000,
							success() {
								setTimeout(() => {
									_this.isStop = true;
								}, 1500)
							}
						})
					}
				})

			},

			updateGPS1: function() {
				let _this = this;
				uni.getLocation({
					type: 'gcj02',
					isHighAccuracy: true,
					altitude: true,
					highAccuracyExpireTime: 5000,
					accuracy: 50,
					async success(res) {
						_this.circles[0].latitude = _this.params.latitude;
						_this.circles[0].longitude = _this.params.longitude;


						if (res.accuracy < _this.params.accuracy) {
							_this.params.speed = res.speed;
							_this.params.accuracy = res.accuracy;
							_this.params.latitude = res.latitude;
							_this.params.longitude = res.longitude;
						}

						if (res.accuracy > 30 && _this.params.times1 < 2) {
							++_this.params.times1;
							_this.local_timer = setTimeout(() => {
								_this.isStop = true;
								_this.updateGPS();
							}, 1000)
						} else {
							_this.params.times1 = 1;
							let local_res = {};
							let params = {
								'type': 1,
								'fid': _this.params.fid,
								'p_id': _this.params.pid,
								'speed': _this.params.speed,
								'accuracy': _this.params.accuracy,
								'gcj02_lat': res.latitude,
								'gcj02_lng': res.longitude,
								'create_uid': _this.userInfo.id,
								'timesid': _this.params.type == 'jl' ? _this.params.timesid : 0
							}
							if (_this.params.type == 'jc') {
								local_res = await _this.$api.pro_api.pcnestUpdateGps(params);
							}

							if (_this.params.type == 'jl') {
								local_res = await _this.$api.pro_api.jlnestUpdateGps(params);
							}
							if (_this.params.type == 'publicpc') {
								local_res = await _this.$api.pro_api.commonnestUpdateGps(params);
							}


							if (local_res.data.ret == 0) {
								uni.hideLoading();
								_this.params.nestNum += 1;
								_this.params.accuracy = res.accuracy;
								_this.params.latitude = res.latitude;
								_this.params.longitude = res.longitude;
								_this.params.localtion = d2Dms(parseFloat(res.longitude)) + '，' + d2Dms(
									parseFloat(res.latitude));


								_this.circles[0].latitude = res.latitude;
								_this.circles[0].longitude = res.longitude;

								_this.markers[0] = {
									id: 0,
									latitude: parseFloat(res.latitude),
									longitude: parseFloat(res.longitude),
									iconPath: '/static/images/my_local.png',
								}


								_this.marker_id += 1;

								let marker = {
									id: _this.marker_id,
									latitude: parseFloat(res.latitude),
									longitude: parseFloat(res.longitude),
									iconPath: '/static/images/mark9.png'
								}

								_this.markers.push(marker);

								uni.setStorage({
									key: 'markers_' + _this.params.pid,
									data: _this.markers
								})

								uni.showToast({
									icon: "success",
									title: "定位成功",
									mask: true,
									duration: 1000,
									success() {

										_this.loading = true;
										setTimeout(() => {
											_this.loading = false;
											_this.isStop = true;
										}, 2000)
									}
								})

							} else {
								uni.hideLoading();
								uni.showToast({
									icon: "success",
									title: res.data.message,
									mask: true,
									duration: 1000,
									success() {
										setTimeout(() => {
											_this.isStop = true;
										}, 10000)
									}
								})
							}
						}
					},
					fail() {
						uni.hideLoading();
						uni.showToast({
							icon: "success",
							title: "定位失败",
							mask: true,
							duration: 1000,
							success() {
								setTimeout(() => {
									_this.isStop = true;
								}, 1500)
							}
						})
					}
				})

			},


			// 获取位置
			getLocal: function() {
				let _this = this;
				uni.showLoading({
					title: "获取位置中..."
				})
				uni.getLocation({
					type: 'gcj02',
					isHighAccuracy: true,
					altitude: true,
					accuracy: 50,
					success: function(res) {
						uni.hideLoading();
						_this.params.accuracy = res.accuracy;
						_this.params.latitude = res.latitude;
						_this.params.longitude = res.longitude;
						_this.params.speed = res.speed;
						_this.params.altitude = res.altitude;

						_this.circles[0].latitude = _this.params.latitude;
						_this.circles[0].longitude = _this.params.longitude;

						_this.markers[0] = {
							id: 0,
							latitude: parseFloat(_this.params.latitude),
							longitude: parseFloat(_this.params.longitude),
							iconPath: '/static/images/my_local.png',
						}


						_this.getCityInfo(res.latitude, res.longitude); // 根据坐标获取城市信息

						_this.addLocus();

					},
					fail() {
						uni.hideLoading();
						uni.showToast({
							icon: "none",
							title: '获取位置失败',
							mask: true,
							duration: 2000,
							success() {}
						})
					}
				});
			},

			// 更新轨迹点
			updateLocus: function() {
				let _this = this;
				uni.getLocation({
					type: 'gcj02',
					isHighAccuracy: true,
					altitude: true,
					highAccuracyExpireTime: 5000,
					accuracy: 50,
					success: function(res) {
						//  判断是否相同地址范围 设置位置偏差
						let difx = Math.abs(_this.params.prevlongitude - res.longitude);
						let dify = Math.abs(_this.params.prevlatitude - res.latitude);

						if (res.accuracy <= 30) {
							if ((_this.params.prevlongitude == '' || _this.params.prevlatitude == '') || (
									difx > 0.00005 && difx < 0.0035) || (dify > 0.00005 && dify <
									0.0035)) {
								console.log('位置变化')
								_this.params.accuracy = res.accuracy;
								_this.params.latitude = res.latitude;
								_this.params.longitude = res.longitude;
								_this.params.speed = res.speed;
								_this.params.altitude = res.altitude;

								_this.circles[0].latitude = _this.params.latitude;
								_this.circles[0].longitude = _this.params.longitude;

								_this.markers[0] = {
									id: 0,
									latitude: parseFloat(res.latitude),
									longitude: parseFloat(res.longitude),
									iconPath: '/static/images/my_local.png',
								}

								let local = {
									latitude: parseFloat(res.latitude),
									longitude: parseFloat(res.longitude),
								}
								_this.polylines[0].points.push(local);


								_this.addLocus();
							}
						} else {
							console.log('无效点')
							clearInterval(_this.timer);
							setTimeout(() => {
								// 10s获取1次位置
								_this.timer = setInterval(() => {
									_this.updateLocus();
								}, 10000)
							}, 3000)
						}
					}
				});
			},

			// 设置诱集器数量
			setTrappersCount: function(e) {
				let data = parseInt(e.detail.value);
				this.params.ant_counts = {};
				if (data >= 20) {
					this.params.trappers_count = 20;
					uni.showToast({
						icon: "none",
						title: "最多输入20个诱集器",
						mask: true
					})
				} else {
					this.params.trappers_count = parseInt(e.detail.value);
				}
				for (let i = 0; i < this.params.trappers_count; i++) {
					this.$set(this.params.ant_counts, (i + 1), '')
				}
			},

			//获取天气信息
			getWeatherInfo: async function(adcode) {
				let res = await this.$api.yfk_api.getWeather({
					key: gd_map_Key_web,
					city: adcode
				})
				if (res != false && res.data.info == "OK") {
					if (res.data.lives.length > 0) {
						this.params.weather = res.data.lives[0].weather;
						this.params.temperature = res.data.lives[0].temperature;
						this.params.humidity = res.data.lives[0].humidity;
					}
				}

			},

			// 获取城市信息
			getCityInfo: async function(lat, lng) {
				let res = await this.$api.yfk_api.getCityInfo({
					key: gd_map_Key_web,
					location: lng + ',' + lat
				})
				if (res.data.status == 1) {
					this.params.address = res.data.regeocode.formatted_address;
					this.getWeatherInfo(res.data.regeocode.addressComponent.adcode); // 根据adcode 获取天气信息
				}
			},


			// 获取生境类型
			getEnvtype: async function() {
				let res = await this.$api.yfk_api.getEnvtype();
				if (res.data.ret == 0) {
					res.data.data.envtypes.push('其他')
					this.envtypes = res.data.data.envtypes;
					if (!uni.getStorageSync('pro_jc_data').env_type) {
						this.params.envtype = res.data.data.envtypes[0];
					}
				}
			},

			// 上传图片
			uploadImage: function() {
				// 上传现场照片
				let _this = this;
				let params = {
					lat: this.params.latitude,
					lng: this.params.longitude,
					location: this.params.location,
					address: this.params.address,
					weather: this.params.weather
				};
				// JSON.parse(decodeURIComponent(option.item));
				params = encodeURIComponent(JSON.stringify(params));
				uni.navigateTo({
					url: '/package_common/photograph/photograph?params=' + params,
					animationType: 'none'
				})


				// uploadImage({
				// 	length: _this.params.files.length,
				// 	maxSize: 1,
				// 	sourceType: ['camera'],
				// 	success(res) {
				// 		let params_data = {};
				// 		params_data.url = res.tempFilePaths[0];
				// 		params_data.status = 0;
				// 		_this.params.files.push(params_data);
				// 		uploadToServe({
				// 			url: '/yfk/upload-file',
				// 			filePath: res.tempFilePaths[0],
				// 			name: 'photo',
				// 			formData: {
				// 				'p_id': 0,
				// 				'uid': _this.userInfo.id
				// 			}
				// 		}).then((up_res) => {
				// 			if (up_res.ret == 0) {
				// 				params_data.file_name = up_res.data.file_name;
				// 				params_data.file_path = up_res.data.file_path;
				// 				params_data.file_type = up_res.data.file_type;
				// 				params_data.status = up_res.data.status;
				// 				params_data.type = up_res.data.type;
				// 			} else {}
				// 		}).catch((err) => {
				// 			console.log('err', err)
				// 		})
				// 	}
				// })
			},

			upload_tpimg() {
				let params_data = {},
					_this = this;
				params_data.url = getApp().globalData.upload_img;
				params_data.status = 0;
				this.params.files.push(params_data);
				console.log('upload-img', params_data.url);

				uploadToServe({
					url: '/yfk/upload-file',
					filePath: params_data.url,
					name: 'photo',
					formData: {
						'p_id': 0,
						'uid': _this.userInfo.id
					}
				}).then((up_res) => {
					if (up_res.ret == 0) {
						params_data.file_name = up_res.data.file_name;
						params_data.file_path = up_res.data.file_path;
						params_data.file_type = up_res.data.file_type;
						params_data.status = up_res.data.status;
						params_data.type = up_res.data.type;
					} else {}
				}).catch((err) => {
					console.log('err', err)
				})
			},

			// 预览图片
			previewPic: function(index) {
				let _this = this;
				let preview_data = [];
				this.params.files.forEach(item => {
					preview_data.push(item.url);
				})
				previewImage({
					data: preview_data,
					index: index
				})
			},

			// 删除图片
			delPic: async function(index) {
				let res = await this.$api.ycj_api.deletefile({
					file_path: this.params.files[index].file_path
				})
				if (res.data.ret == 0) {
					this.params.files.splice(index, 1);
				}
			},


			// 切换生境
			changeEnvtypes: function(e) {
				if (e.detail.value == this.envtypes.length - 1) {
					this.params.envtype = '';
				} else {
					this.params.envtype = this.envtypes[e.detail.value];
				}
			},

			// 提交数据
			submit: async function() {
				let _this = this;

				let antCounts = [];
				for (let i in this.params.ant_counts) {
					if (!this.params.ant_counts[i]) {
						uni.showToast({
							icon: 'none',
							title: '请输入诱集器-' + i + '工蚁数量',
							mask: true,
							duration: 1000
						})
						return
					}
					antCounts.push(this.params.ant_counts[i]);
				}


				if (this.params.files.length == 0) {
					uni.showToast({
						icon: 'none',
						title: '请上传现场照片',
						mask: true,
						duration: 1000
					})
					return
				};

				for (let i in this.params.files) {
					if (this.params.files[i].status == 0) {
						uni.showToast({
							icon: 'none',
							title: '照片还未上传完',
							mask: true,
							duration: 1000
						})
						return false;
					}
				}



				if (this.isStop) {
					this.isStop = false;

					let params = {
						type: 2,
						files: JSON.stringify(this.params.files),
						p_id: this.params.pid,
						fid: this.params.fid,
						speed: this.params.speed,
						accuracy: this.params.accuracy,
						gcj02_lat: this.params.latitude,
						gcj02_lng: this.params.longitude,
						address: this.params.address,
						env_type: this.params.envtype,
						antCounts: JSON.stringify(antCounts),
						create_uid: this.userInfo.id,
						weather: this.params.weather,
						temperature: this.params.temperature,
						humidity: this.params.humidity,
						altitude: this.params.altitude,
						remark: this.params.remark,
					}
					let res = {};
					if (this.params.pid == 0) {
						res = await this.$api.pro_api.addCommonProdata(params);
					} else {
						if (this.params.work_type == "group") {
							res = await this.$api.pro_api.addCommonProdata(params);
						} else {
							res = await this.$api.pro_api.addPcdata(params);
						}
					}

					uni.showLoading({
						title: "数据提交中...",
						mask: true
					})
					if (res.data.ret == 0) {
						this.updateCollectData(res.data.data);
						this.params.fid = 0; 
						setTimeout(()=>{
							uni.showToast({
								icon:"success",
								title: "数据提交成功",
								mask: true,
								duration: 1000,
								success() {
									uni.hideLoading();
									_this.isSaveData=true;					
									setTimeout(() => {
										_this.isStop = true;
										uni.navigateBack({
											delta: 1
										})
									}, 1100)
																		
									uni.setStorage({
										key: 'pro_jc_data',
										data: {
											env_type: _this.params.envtype,
											remark: _this.params.remark
										}
									})
																		
									uni.removeStorageSync('prev_jc_' + _this.params.pid); // 清除上次未提交数据
									uni.removeStorageSync('markers_' + _this.params.pid); // 清除蚁巢地图marker点
																		
									_this.$store.commit('UPDATE_PRO_JC_INFO', 1);   // 添加成功更新项目详情 							
								}
							})
						},1000)
 
					} else {
						uni.hideLoading();
						uni.showToast({
							image: '/static/images/error.png',
							title: res.data.message,
							mask: true,
							duration: 1000,
							success() {
								setTimeout(() => {
									_this.isStop = true;
								})
							}
						})
					}

				}
			},

			//上传轨迹信息
			addLocus: async function() {
				let _this = this;
				uni.setKeepScreenOn({
					keepScreenOn: true
				});
				let params = {
					'uid': this.userInfo.id,
					'pid': this.params.pid,
					'fid': this.params.fid,
					'speed': this.params.speed,
					'accuracy': this.params.accuracy,
					'gcj02_lat': this.params.latitude,
					'gcj02_lng': this.params.longitude,
				};
				let res = {}
				if (this.params.pid == 0) { // pid有值  为项目数据  否则 公共数据
					res = await this.$api.pro_api.addCommonLocus(params);
					console.log(1, res)
				} else {
					if (this.params.work_type == 'group') {
						res = await this.$api.pro_api.addCommonLocus(params);
						console.log(2, res)
					} else {
						res = await this.$api.pro_api.addProjectLocus(params);
						console.log(3, res)
					}

				}
				console.log(1, res)
				if (res.data.ret == 0) {
					this.params.prevlatitude = this.params.latitude;
					this.params.prevlongitude = this.params.longitude; 
					if (this.params.fid == 0 && res.data.data > 0 && !this.prev_data.fid) {
						this.params.fid = res.data.data;  
					}

				}
			},


			// 更新数据
			updateCollectData: async function(id) {
				let _this = this; 
				let updateState=0;
				let res_collect={},
					dc_area={},
					res_adcode={},
					user_locus={};
					   
				// 计算发生面积
				if (this.params.pid == 0) {
					res_collect = await this.$api.pro_api.addCommonProUpdateCollectData({
						id: id
					});
				} else {
					if (this.params.work_type == 'group') {
						res_collect = await this.$api.pro_api.addCommonProUpdateCollectData({
							id: id
						});
					} else {
						res_collect = await this.$api.pro_api.addPcproUpdateCollectData_new({
							id: id
						});
					} 
				}
				if (res_collect.data.ret == 0) {
					updateState += 1;
				}  
				 
				//计算调查面积
				if (this.params.pid == 0) {
					dc_area = await this.$api.pro_api.addCommonProDcArea_new({
						id: id
					});
				} else {
					if (this.params.work_type == 'group') {
						dc_area = await this.$api.pro_api.addCommonProDcArea_new({
							id: id
						});
					} else {
						dc_area = await this.$api.pro_api.addPcproCountDcArea_new({
							id: id
						});
					} 
				}
				if (dc_area.data.ret == 0) {
					updateState += 1;
				}  
				if(dc_area.data.ret==0){
					updateState+=1;
				} 


				// 更新地区代码
				if (this.params.pid == 0) { // pid有值  为项目数据  否则 公共数据
					res_adcode = await this.$api.pro_api.addCommonproAdcode();
				} else {
					if (this.params.work_type == 'group') {
						res_adcode = await this.$api.pro_api.addCommonproAdcode();
					} else {
						res_adcode = await this.$api.pro_api.addPcproAdcode(); 
					}
				}
				if (res_adcode.data.ret == 0) {
					updateState += 1;
				}  
				if(res_adcode.data.ret==0){
					updateState+=1;
				} 


				//添加用户轨迹
				if (this.params.pid == 0) {
					user_locus = await _this.$api.pro_api.addCommonProLocus({
						uid: _this.userInfo.id,
						pid: _this.params.pid,
					});
				} else {
					if (this.params.work_type == 'group') {
						user_locus = await _this.$api.pro_api.addCommonProLocus({
							uid: _this.userInfo.id,
							pid: _this.params.pid,
						});
					} else {
						user_locus = await _this.$api.pro_api.addUserProLocus({
							uid: _this.userInfo.id,
							pid: _this.params.pid,
						});
					} 
				}
				if (user_locus.data.ret == 0) {
					updateState += 1;
				}  
				if(user_locus.data.ret==0){
					updateState+=1;
				}  
			},

		},
		components: {},

		computed: {
			isUpload() {
				return this.$store.state.upload_state
			}
		},
		watch: {
			isUpload(newVal, oldVal) {
				if (newVal) {
					console.log('开始上传图片');
					this.$store.commit('UPDATE_UPLOAD_STATE', false);
					this.upload_tpimg();
				}
			}
		}

	}
</script>

<style lang="less" scoped>
	.map {
		height: 400rpx;
		background: #ccc;
	}

	.form-wrap .label {
		width: 35%;
	}

	.form-wrap .content .link {
		color: #000;
	}

	.set-local-btn {
		border-radius: 50%;
		width: 250rpx;
		height: 250rpx;
		line-height: 250rpx;
		margin: 70rpx auto;
		background-image: linear-gradient(to right, #fe5d55, #e2231a);
		box-shadow: 0 0 10rpx #000;
		font-size: 40rpx;
		text-align: center;
		color: #fff;
	}

	.local-btn {
		position: absolute;
		right: 50rpx;
		top: 5rpx;
		padding: 3rpx 8rpx;
		background: #f00;
		border-radius: 5rpx;
		color: #fff;
		font-size: 30rpx;
	}

	.status {
		padding: 0 20rpx;
		color: #f00;
		font-size: 28rpx;
	}

	.hd-title {
		padding: 20rpx;
		font-size: 32rpx;
		font-weight: bold;
	}

	.leavel_1 {
		color: #09f
	}

	.leavel_2 {
		color: #ff9600;
	}

	.leavel_3 {
		color: #f00;
	}

	.btn {
		@h: 80rpx;
		width: 98%;
		margin: 10rpx auto;
		text-align: center;
		height: @h;
		line-height: @h;
		background: linear-gradient(#e96b5c, #ce3c2f);
		border-radius: 10rpx;
		color: #fff;
		font-size: 34rpx;
	}

	.prev-btn {
		text-align: right;
		padding: 20rpx;

		text {
			padding: 4rpx 15rpx;
			background: #ff9600;
			border-radius: 8rpx;
			color: #fff;
			font-size: 32rpx;
		}
	}

	.prev-data {
		padding: 18rpx 10rpx;
		background: #f5f5f5;
		text-align: right;
		color: #999;
		font-size: 28rpx;

		.val {
			color: #333;
		}

		.use-btn {
			color: #d03c30;
		}
	}
</style>
